<?php

/**
* TwitterModule.class.php
*
* Module responsable for code hunt
*
* @author     Blazej Zieba <b@zieba.info>
* @copyright  2007 konekto.com
* @version    CVS: $Id:$
*/
require_once("Auth/Auth.php");
require_once("framework/config.php");
require_once("UserAuth.php");
require_once("framework/PageModule.class.php");
require_once("HTML/QuickForm.php");
require_once("HTML/QuickForm/Renderer/Default.php");
require_once("HTML/Template/IT.php");
require_once("Services/Twitter.php");


// {{{ AuthForm

class TwitterModule extends PageModule 
{
    
    
    private $dbh = null;

    function TwitterModule()
    {

        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN'); 
        $this->dbh = new PDO($options['dsn_string']);
        $this->dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

    }

    // {{{ getContent()

    
    /**
    * getContent()
    *
    */

    function getContent()
    {

        trigger_error("ENTER TwitterModule::getContent()", E_USER_NOTICE);

        $twitterOpt = &PEAR::getStaticProperty('CONFIG_INI','TWITTER');
        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL'); 
        $options = &PEAR::getStaticProperty('CONFIG_INI','TEMPLATES');        
        $tpl = new HTML_Template_IT($options["templates_dir"]);
        $tpl->loadTemplatefile("twitter.tpl.xhtml",true,true);

        $statusTpl = new HTML_Template_IT($options["templates_dir"]);
        $statusTpl->loadTemplatefile("status.tpl.xhtml",FALSE,FALSE);

        $twitter = new Services_Twitter($twitterOpt['username'],$twitterOpt['password']);
        
        if (isSet($global['proxy']))
        {
            $data = split(':',$global['proxy']);
            $request = new HTTP_Request2();
            $request->setConfig('proxy_host',$data[0]);    
            $request->setConfig('proxy_port',$data[1]);    
            $twitter->setRequest($request);
        }

        $response = $twitter->statuses->mentions();

        if ( count($response) )
        {
            foreach($response as $status )
            {
                $tpl->setCurrentBlock('twitter_row');
                $timeStamp = strtotime($status->created_at); 
                $tpl->setVariable("DAY",date('d',$timeStamp));
                $tpl->setVariable("MONTH",date('m',$timeStamp));
                $tpl->setVariable("YEAR",date('Y',$timeStamp));
                $tpl->setVariable("HOUR",date('H',$timeStamp));
                $tpl->setVariable("MINUTE",date('i',$timeStamp));
                $tpl->setVariable("SECOND",date('s',$timeStamp));
                $tpl->setVariable("TIMEZONE",date('T',$timeStamp));
                $tpl->setVariable("USERNAME",$status->user->name);
                $tpl->setVariable("TEXT",$status->text);
                $tpl->parseCurrentBlock('twitter_row');       
                $tpl->parse("row");
            }

        }
        else
        {
            $tpl->setCurrentBlock('empty_row');
            $tpl->setVariable("INFO",_('Brak wpisów'));
            $tpl->parseCurrentBlock('empty_row');       
            $tpl->parse("row");
        }

        $statusTpl->setVariable("STATUS",sprintf(_("Odpowiedzi do @%s"),$twitterOpt['username']));
        return $statusTpl->get().$tpl->get().$statusTpl->get();
    }
    
    // }}}
    

}

// }}} 

?>

